<template>
  <div>
      <!-- 7-9 重点是理解Vue中父子组件之间的传值实际应用 -->
      <home-header></home-header>
      <home-swiper :list='swiperList'></home-swiper>
      <home-icons :list='iconList'></home-icons>
      <home-recommend :list='recommendList'></home-recommend>
      <home-weekend :list='weekendList'></home-weekend>
  </div>
</template>

<script>
import HomeHeader from './components/Header'
import HomeSwiper from './components/Swiper'
import HomeIcons from './components/Icons'
import HomeRecommend from './components/Recommend'
import HomeWeekend from './components/Weekend'
// 使用axios之前需要导入
import axios from 'axios'
import { mapState } from 'vuex'
export default {
  name: 'Home',
  data () {
    return {
      swiperList: [],
      iconList: [],
      recommendList: [],
      weekendList: [],
      lastCity: ''
    }
  },
  computed: {
    ...mapState(['city'])
  },
  components: {
      HomeHeader,
      HomeSwiper,
      HomeIcons,
      HomeRecommend,
      HomeWeekend
  },
  // 以下为使用axios的常规步骤
  methods:{
    // axios请求返回的是promise对象，所以应用以下处理方式处理返回数据
    getHomeInfo () {
      axios.get('/static/mock/index.json?city=' + this.city)
        .then(this.getHomeInfoSucc)

    },
    // 重点是要搞懂res的数据结构是怎么样，从而提取出所需要的数据
    getHomeInfoSucc (res) {
      res = res.data
      // 如果后端返回数据代码正常，且有数据。这是常规编程思维之一：尽量避免无谓的报错
      if (res.ret && res.data){
        this.swiperList = res.data.swiperList
        this.iconList = res.data.iconList
        this.recommendList = res.data.recommendList
        this.weekendList = res.data.weekendList
      }
      
    }

  },
  mounted () {
    this.lastCity = this.city
    this.getHomeInfo()
  },
  activated () {
    if (this.lastCity !== this.city) {
      this.lastCity = this.city
      this.getHomeInfo()
    }
    
  }
}
</script>
<style lang='stylus' scoped>
</style>
